package socket_dog;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class Client extends JFrame implements ActionListener {

	/**
	 * Data Members
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X = 550;
	private final int FRAME_Y = 100;
	private final int FRAME_WIDTH = 400;
	private final int FRAME_HEIGHT = 300;
	private JButton sendButton;
	private final int BUTTON_X = 280, BUTTON_Y = 50, BUTTON_WIDTH = 110,
			BUTTON_HEIGHT = 30;
	private Timer timer;
	private JTextArea text;
	private final int TEXT_X = 10, TEXT_Y = 30, TEXT_WIDTH = 260,
			TEXT_HEIGHT = 200;
	/*
	 * Network connections
	 */
	private int port = 5555;
	private Socket connection;
	private String server_address = "192.168.1.6";
	/*
	 * Streams for network connections
	 */
	ObjectInputStream instream;
	ObjectOutputStream outstream;
	Dog out_message, in_message;

	/**
	 * Constructor
	 * 
	 * @param title
	 */
	public Client(String title) {
		setLayout(null);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setBackground(Color.GRAY);

		/*
		 * Add components
		 */
		sendButton = new JButton("Send message");
		sendButton.setBorder(null);
		sendButton.addActionListener(this);
		sendButton.setBounds(BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
		add(sendButton);

		text = new JTextArea();
		text.setBorder(BorderFactory.createLoweredBevelBorder());
		text.setBounds(TEXT_X, TEXT_Y, TEXT_WIDTH, TEXT_HEIGHT);
		text.setLineWrap(true);
		add(text);

		/*
		 * We will use a timer to check for messages every second 1 sec == 1000
		 * msec What the code below will do: it will call actionPerformed once
		 * second
		 */
		timer = new Timer(1000, this);
		timer.start();

		setTitle(title);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		/*
		 * Set up network connections
		 */
		try {
			connection = new Socket(server_address, port);
			outstream = new ObjectOutputStream(connection.getOutputStream());

			System.out.println(" Connected to " + connection.getInetAddress()
					+ ":" + connection.getPort() + " using local port "
					+ connection.getLocalPort());
			instream = new ObjectInputStream(connection.getInputStream());
		} catch (IOException ioe) {
			System.out.println("Not able to connect on port " + port);
		}
	}

	public void paint(Graphics g) {
		super.paint(g);
	}

	public void actionPerformed(ActionEvent arg0) {
		/*
		 * User clicks a button:
		 */
		if (arg0.getSource().equals(sendButton)) {
			System.out.println("Send button clicked");
			if (outstream != null)
				try {
					outstream.writeObject(new Dog((int) (10 * Math.random()),
							100 * Math.random()));
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

		/*
		 * Timer went off
		 */
		if (arg0.getSource().equals(timer)) {
			System.out.print(".");
			try {
				if (instream != null  && connection.getInputStream().available()>0) {
					System.out.println("  input available for reading");
					in_message = (Dog) instream.readObject();
					if (in_message != null) {
						text.append("Message from server> " + in_message + "\n");
						in_message = null;
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new Client("Test Client");
	}
}